package com.lzk.system.controller;

import com.lzk.basic.annotation.PreAuthorize;
import com.lzk.basic.util.JsonResult;
import com.lzk.basic.util.PageList;
import com.lzk.system.domain.Menu;
import com.lzk.system.query.MenuQuery;
import com.lzk.system.service.IMenuService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/menu")

@Api(value = "菜单的API",description="菜单相关的CRUD功能")
public class MenuController {

    @Resource
    private IMenuService menuService;

    @GetMapping("/menuTree")
    @ApiOperation(value = "菜单树" )
    @PreAuthorize(name = "菜单树",sn = "menu:tree")
    public List<Menu> menuTree(){
        return menuService.menuTree();
    }
    /**
     * 查询所有数据
     * @return
     */
    @ApiOperation(value = "查询所有数据" )
    @GetMapping
    public List<Menu> findAll(){
        return menuService.loadAll();
    }

    /**
     * 查询一个 接收参数id
     * @param id
     * @return
     */
    @ApiOperation(value = "查询一个数据" )
    @GetMapping("/{id}")
    public Menu findOne(@PathVariable("id") Long id){
        return menuService.loadById(id);
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @ApiOperation(value = "删除一个数据" )
    @DeleteMapping("/{id}")
    public JsonResult delete(@PathVariable("id") Long id){
        try {
            menuService.remove(id);
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return  JsonResult.me(false,"操作失败");
        }
    }

    /**
     * 添加或修改
     * @param menu
     * @return
     */
    @ApiOperation(value = "添加或修改数据" )
    @PutMapping
    public JsonResult updateOrAdd(@RequestBody Menu menu){
        try {
            if (menu.getId() == null){
                menuService.save(menu);
            }else {
                menuService.update(menu);
            }
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return  JsonResult.me(false,"操作失败");
        }
    }
    /**
     * 分页查询或高级查询
     * @param menuQuery
     * @return
     */
    @ApiOperation(value = "分页查询或高级查询" )
    @PostMapping
    public PageList<Menu> queryPage(@RequestBody MenuQuery menuQuery){
        return menuService.queryData(menuQuery);
    }

    /**
     * 批量删除
     * @param ids
     * @return
     */
    @ApiOperation(value = "删除一个数据" )
    @PatchMapping
    public JsonResult patchDelete(@RequestBody List<Long> ids){
        try {
            menuService.patchRemove(ids);
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return  JsonResult.me(false,"操作失败");
        }
    }
}
